﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Meta.Quote;
using AAA.Meta.Quote.Data;

namespace AAA.TradeLanguage
{
    public abstract class AbstractFunction : DefaultDataAccess, IFunction
    {
        private IFunctionMeta _functionMeta;
        private BarCompression _barCompression;
        private string _strBaseSymbolId;


        public AbstractFunction()
        {
            _functionMeta = new DefaultFunctionMeta();
        }

        #region IFunction Members


        public BarRecord Calculate()
        {
            return Calculate(null);
        }

        public BarRecord Calculate(string strName)
        {
            BarRecord returnRecord = ExecCalculate();
            DateTime dtBarTime;
            DateTime dtDataBarTime;
            
            if (strName != null && CurrentTime != null)
            {
                dtBarTime = CurrentTime.CurrentBarTime(CurrentTime.CurrentDateTime, DataCompression);
                dtDataBarTime = CurrentTime.CurrentBarTime(Time(strName, 0), DataCompression);

                returnRecord.BarCompression = DataCompression.DataCompression;
                returnRecord.CompressionInterval = DataCompression.Interval;
                returnRecord.BarDateTime = dtBarTime;
                returnRecord.SymbolId = strName;

                if ((dtDataBarTime.CompareTo(dtBarTime) < 0) || (DataSource.ContainsSymbol(strName) == false))
                {
                    AddBarData(strName, returnRecord);
                }
                else
                {
                    UpdateLastBarData(strName, returnRecord);
                }
            }

            return returnRecord;
        }

        public abstract BarRecord ExecCalculate();

        public string BaseSymbolId
        {
            get
            {
                return _strBaseSymbolId;
            }

            set
            {
                _strBaseSymbolId = value;
                _barCompression = BarCompression(_strBaseSymbolId);
            }
        }

        public BarCompression DataCompression
        {
            get
            {
                return _barCompression;
            }
        }

        public void SetCurrentTime(CurrentTime currentTime)
        {
            CurrentTime = currentTime;
            DataSource = currentTime.DataSource;
        }


        #endregion

        #region IFunctionMeta Members

        public bool IsActive
        {
            get
            {
                return _functionMeta.IsActive;
            }
            set
            {
                _functionMeta.IsActive = value;
            }
        }

        public string ErrorMessage
        {
            get
            {
                return _functionMeta.ErrorMessage;
            }
            set
            {
                _functionMeta.ErrorMessage = value;
            }
        }

        public string DisplayName
        {
            get
            {
                return _functionMeta.DisplayName;
            }
            set
            {
                _functionMeta.DisplayName = value; ;
            }
        }

        public string[] InputVariableNames
        {
            get
            {
                return _functionMeta.InputVariableNames;
            }
            set
            {
                _functionMeta.InputVariableNames = value;
            }
        }

        public string[] InputVariableDescs
        {
            get
            {
                return _functionMeta.InputVariableDescs;
            }
            set
            {
                _functionMeta.InputVariableDescs = value;
            }
        }

        public object[] DefaultValues
        {
            get
            {
                return _functionMeta.DefaultValues;
            }
            set
            {
                _functionMeta.DefaultValues = value;
            }
        }

        public void InputVariable(string strName, object oValue)
        {
            _functionMeta.InputVariable(strName, oValue);
        }

        public object InputVariable(string strName)
        {
            return _functionMeta.InputVariable(strName);
        }

        #endregion
    }
}
